package com.spzx.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.spzx.common.core.exception.ServiceException;
import com.spzx.product.domain.ProductSpec;
import com.spzx.product.mapper.ProductSpecMapper;
import com.spzx.product.service.ProductSpecService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;

/**
 * <p>
 * 商品规格 服务实现类
 * </p>
 *
 * @author Haoran Sun
 * @since 2024-07-15
 */
@Service
public class ProductSpecServiceImpl extends ServiceImpl<ProductSpecMapper, ProductSpec> implements ProductSpecService {

    @Resource
    private ProductSpecMapper productSpecMapper;
    @Resource
    private CategoryServiceImpl categoryService;

    @Override
    public List<ProductSpec> listProductSpecByQuery(ProductSpec productSpec) {
        return productSpecMapper.listProductSpecByQuery(productSpec);
    }

    @Override
    public ProductSpec getInfoById(Long id) {

        ProductSpec productSpec = productSpecMapper.selectOne(
                new LambdaQueryWrapper<ProductSpec>()
                        .eq(ProductSpec::getId, id)
                        .select(ProductSpec::getId,
                                ProductSpec::getSpecName,
                                ProductSpec::getSpecValue,
                                ProductSpec::getCategoryId));
        if (productSpec != null) {
            LinkedList<Long> list = new LinkedList<>();
            categoryService.getParentCategory(productSpec.getCategoryId(),list);
            productSpec.setCategoryIdList(list);
            return productSpec;
        }
        throw new ServiceException("商品规格不存在");
    }

    @Override
    public List<ProductSpec> getProductSpecListById(Long categoryId) {
        return productSpecMapper.selectList(
            new LambdaQueryWrapper<ProductSpec>()
                    .eq(ProductSpec::getCategoryId, categoryId)
                    .select(ProductSpec::getId,
                            ProductSpec::getCategoryId,
                            ProductSpec::getSpecName,
                            ProductSpec::getSpecValue));
    }
}
